package com.ouc.openplatform.zykt.standardManage.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.ouc.openplatform.zykt.standardManage.dto.NJ_TestStandardSubitermDTO;
import com.ouc.openplatform.zykt.standardManage.model.NJ_TestItemTemplet;
import com.ouc.openplatform.zykt.standardManage.model.NJ_TestStandardSubiterm;
import com.ouc.openplatform.zykt.standardManage.service.NJ_ClientTestStandardSubitermService;
import com.ouc.openplatform.zykt.standardManage.service.NJ_TestStandardSubitermService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class NJ_ClientTestStandardSubitermServiceImpl implements
		NJ_ClientTestStandardSubitermService {

	NJ_TestStandardSubitermService testStandardSubitermService;

	public NJ_TestStandardSubitermService getTestStandardSubitermService() {
		return testStandardSubitermService;
	}

	public void setTestStandardSubitermService(
			NJ_TestStandardSubitermService testStandardSubitermService) {
		this.testStandardSubitermService = testStandardSubitermService;
	}

	/**
	 * 对象转换方法
	 * */
	private NJ_TestStandardSubitermDTO transformTssDTO(
			NJ_TestStandardSubiterm testStandardSubiterm) {
		NJ_TestStandardSubitermDTO testStandardSubitermDTO = new NJ_TestStandardSubitermDTO();

		testStandardSubitermDTO.setId(testStandardSubiterm.getId());
		testStandardSubitermDTO.setName(testStandardSubiterm.getName());
		testStandardSubitermDTO.setStandardItermID(testStandardSubiterm
				.getStandardItermID());
		testStandardSubitermDTO.setItem_name(testStandardSubiterm
				.getItem_name());
		testStandardSubitermDTO.setTestInDry(testStandardSubiterm
				.getTestInDry());
		testStandardSubitermDTO.setInDryUnit(testStandardSubiterm
				.getInDryUnit());
		testStandardSubitermDTO.setTestInDamp(testStandardSubiterm
				.getTestInDamp());
		testStandardSubitermDTO.setInDampUnit(testStandardSubiterm
				.getInDampUnit());
		testStandardSubitermDTO.setTestOutDry(testStandardSubiterm
				.getTestOutDry());
		testStandardSubitermDTO.setOutDryUnit(testStandardSubiterm
				.getOutDryUnit());
		testStandardSubitermDTO.setTestOutDamp(testStandardSubiterm
				.getTestOutDamp());
		testStandardSubitermDTO.setOutDampUnit(testStandardSubiterm
				.getOutDampUnit());
		testStandardSubitermDTO.setNeedTime(testStandardSubiterm.getNeedTime());
		testStandardSubitermDTO.setTestTime(testStandardSubiterm.getTestTime());
		testStandardSubitermDTO.setAddUser(testStandardSubiterm.getAddUser());
		testStandardSubitermDTO.setAddTime(testStandardSubiterm.getAddTime());
		testStandardSubitermDTO.setMark(testStandardSubiterm.getMark());
		testStandardSubitermDTO.setDatatype(testStandardSubiterm.getDatatype());
		testStandardSubitermDTO.setMax(testStandardSubiterm.getMax());
		testStandardSubitermDTO.setMin(testStandardSubiterm.getMin());
		testStandardSubitermDTO.setUnit(testStandardSubiterm.getUnit());

		return testStandardSubitermDTO;
	}

	private NJ_TestStandardSubiterm transformTss(
			NJ_TestStandardSubitermDTO testStandardSubitermDTO) {
		NJ_TestStandardSubiterm testStandardSubiterm = new NJ_TestStandardSubiterm();

		testStandardSubiterm.setId(testStandardSubitermDTO.getId());
		testStandardSubiterm.setName(testStandardSubitermDTO.getName());
		testStandardSubiterm.setStandardItermID(testStandardSubitermDTO
				.getStandardItermID());
		testStandardSubiterm.setItem_name(testStandardSubitermDTO
				.getItem_name());
		testStandardSubiterm.setTestInDry(testStandardSubitermDTO
				.getTestInDry());
		testStandardSubiterm.setInDryUnit(testStandardSubitermDTO
				.getInDryUnit());
		testStandardSubiterm.setTestInDamp(testStandardSubitermDTO
				.getTestInDamp());
		testStandardSubiterm.setInDampUnit(testStandardSubitermDTO
				.getInDampUnit());
		testStandardSubiterm.setTestOutDry(testStandardSubitermDTO
				.getTestOutDry());
		testStandardSubiterm.setOutDryUnit(testStandardSubitermDTO
				.getOutDryUnit());
		testStandardSubiterm.setTestOutDamp(testStandardSubitermDTO
				.getTestOutDamp());
		testStandardSubiterm.setOutDampUnit(testStandardSubitermDTO
				.getOutDampUnit());
		testStandardSubiterm.setNeedTime(testStandardSubitermDTO.getNeedTime());
		testStandardSubiterm.setTestTime(testStandardSubitermDTO.getTestTime());
		testStandardSubiterm.setAddUser(testStandardSubitermDTO.getAddUser());
		testStandardSubiterm.setAddTime(testStandardSubitermDTO.getAddTime());
		testStandardSubiterm.setMark(testStandardSubitermDTO.getMark());
		testStandardSubiterm.setDatatype(testStandardSubitermDTO.getDatatype());
		testStandardSubiterm.setMax(testStandardSubitermDTO.getMax());
		testStandardSubiterm.setMin(testStandardSubitermDTO.getMin());
		testStandardSubiterm.setUnit(testStandardSubitermDTO.getUnit());

		return testStandardSubiterm;
	}

	SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public JSONObject tssDtoToJsonObj(NJ_TestStandardSubitermDTO tssDTO) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("id", tssDTO.getId());
		jsonObject.put("name", tssDTO.getName());
		jsonObject.put("standardItermID", tssDTO.getStandardItermID());
		jsonObject.put("item_name", tssDTO.getItem_name());
		jsonObject.put("testInDry", tssDTO.getTestInDry());
		jsonObject.put("inDryUnit", tssDTO.getInDryUnit());
		jsonObject.put("testInDamp", tssDTO.getTestInDamp());
		jsonObject.put("inDampUnit", tssDTO.getInDampUnit());
		jsonObject.put("testOutDry", tssDTO.getTestOutDry());
		jsonObject.put("outDryUnit", tssDTO.getOutDryUnit());
		jsonObject.put("testOutDamp", tssDTO.getTestOutDamp());
		jsonObject.put("outDampUnit", tssDTO.getOutDampUnit());
		jsonObject.put("needTime", tssDTO.getNeedTime());
		jsonObject.put("testTime", tssDTO.getTestTime());
		jsonObject.put("addUser", tssDTO.getAddUser());
		try{
        	jsonObject.put("addTime", formatter.format(formatter.parse(tssDTO.getAddTime())));
        }catch (Exception e) {
			// TODO: handle exception
		}
//		jsonObject.put("addTime", tssDTO.getAddTime());
		jsonObject.put("mark", tssDTO.getMark());
		jsonObject.put("datatype", tssDTO.getDatatype());
		jsonObject.put("max", tssDTO.getMax());
		jsonObject.put("min", tssDTO.getMin());
		jsonObject.put("unit", tssDTO.getUnit());

		return jsonObject;
	}

	/**
	 * 重写方法
	 * */
	@Override
	public Object getAllTestStandardSubiterm(int standardItermID) {
		System.out.println("enter getData function,standardItermID:"
				+ standardItermID);
		List<NJ_TestStandardSubitermDTO> tssDTOList = new ArrayList<NJ_TestStandardSubitermDTO>();
		tssDTOList = searchAllTestStandardSubiterm(standardItermID);
		JSONArray jsonArray = new JSONArray();
		for (NJ_TestStandardSubitermDTO tssDTO : tssDTOList) {
			jsonArray.add(tssDtoToJsonObj(tssDTO));
		}
		System.out.println("jsonArray:" + jsonArray);
		return jsonArray;
	}

	@Override
	public List<NJ_TestStandardSubitermDTO> searchAllTestStandardSubiterm(
			int standardItermID) {
		List<NJ_TestStandardSubitermDTO> tssDTOList = new ArrayList<NJ_TestStandardSubitermDTO>();
		List<NJ_TestStandardSubiterm> tssList = testStandardSubitermService
				.searchAllTestStandardSubiterm(standardItermID);
		for (NJ_TestStandardSubiterm tss : tssList) {
			NJ_TestStandardSubitermDTO tssDTO = transformTssDTO(tss);
			System.out.println(tssDTO.getName() + tssDTO.getNeedTime()
					+ tssDTO.getTestTime());
			tssDTOList.add(tssDTO);
		}

		return tssDTOList;
	}

	@Override
	public int insertTestStandardSubiterm(NJ_TestStandardSubitermDTO subitermDTO) {
		System.out.println(subitermDTO.getName() + subitermDTO.getTestInDry());
		NJ_TestStandardSubiterm testStandardSubiterm = new NJ_TestStandardSubiterm();
		testStandardSubiterm = transformTss(subitermDTO);
		return testStandardSubitermService
				.insertTestStandardSubiterm(testStandardSubiterm);
	}

	@Override
	public int editTestStandardSubiterm(NJ_TestStandardSubitermDTO subitermDTO) {
		NJ_TestStandardSubiterm testStandardSubiterm = new NJ_TestStandardSubiterm();
		testStandardSubiterm = transformTss(subitermDTO);
		return testStandardSubitermService
				.editTestStandardSubiterm(testStandardSubiterm);
	}

	@Override
	public int deleteTestStandardSubiterm(int id, int standardItermID) {
		System.out.println("standardItermID:" + standardItermID);
		return testStandardSubitermService.deleteTestStandardSubiterm(id,
				standardItermID);
	}

	@Override
	public int insertSubItemByExl(String[][] data, String flag, String realname) {
		System.out.println("tss导入Exl");
		List<NJ_TestStandardSubiterm> tssL = new ArrayList<NJ_TestStandardSubiterm>();
		int datarow = data.length; // 数组的总行数
		int result = 0;
		for (int i = 1; i < datarow; i++) {
			NJ_TestStandardSubiterm testStandardSubiterm = new NJ_TestStandardSubiterm();
			for (int j = 1; j < 9; j++) {
				System.out.println("data[" + i + "][" + j + "]:" + data[i][j]);
			}
			if(data[i][1] == null || data[i][1] ==""){
				testStandardSubiterm.setItem_name(" ");
			}else{
				testStandardSubiterm.setItem_name(data[i][1]);
			}
			

			try {
				List<NJ_TestItemTemplet> titList = testStandardSubitermService
						.getItemidByName(data[i][1]);
				for (NJ_TestItemTemplet t : titList) {
					if (t.getTemplate_name().equals(data[i][0])) {
						testStandardSubiterm.setStandardItermID(t.getItem_id());
					}
				}
				System.out.println("data[i][1]:" + data[i][1]);
				System.out.println(testStandardSubiterm.getStandardItermID());
			} catch (Exception e) {
				System.out.println("获取item_id失败");
				testStandardSubiterm.setStandardItermID(0);
			}
			if(data[i][2] == null || data[i][2] ==""){
				testStandardSubiterm.setName(" ");
			}else{
				testStandardSubiterm.setName(data[i][2]);
			}
			if(data[i][3] == null || data[i][3] ==""){
				testStandardSubiterm.setEnglishName(" ");
			}else{
				testStandardSubiterm.setEnglishName(data[i][3]);
			}
			

			try {
				System.out.println("TestInDry:" + data[i][4]);
				testStandardSubiterm.setTestInDry(Integer.parseInt(data[i][4]));
			} catch (Exception e) {
				System.out.println("数据错误");
				testStandardSubiterm.setTestInDry(-1000);
			}

			try {
				System.out.println("TestInDamp:" + data[i][5]);
				testStandardSubiterm
						.setTestInDamp(Integer.parseInt(data[i][5]));
			} catch (Exception e) {
				System.out.println("数据错误");
				testStandardSubiterm.setTestInDamp(-1000);
			}

			try {
				System.out.println("TestOutDry:" + data[i][6]);
				testStandardSubiterm
						.setTestOutDry(Integer.parseInt(data[i][6]));
			} catch (Exception e) {
				System.out.println("数据错误");
				testStandardSubiterm.setTestOutDry(-1000);
			}

			try {
				System.out.println("TestOutDamp:" + data[i][7]);
				testStandardSubiterm.setTestOutDamp(Integer
						.parseInt(data[i][7]));
			} catch (Exception e) {
				System.out.println("数据错误");
				testStandardSubiterm.setTestOutDamp(-1000);
			}

			double totleTime = 0;
			
			try {
				System.out.println("NeedTime:" + data[i][8]);
				testStandardSubiterm
						.setNeedTime(Double.parseDouble(data[i][8]));
				totleTime = totleTime+Double.parseDouble(data[i][8]);
			} catch (Exception e) {
				System.out.println("数据错误");
				testStandardSubiterm.setNeedTime(-1000);
				totleTime = totleTime+0.0;
			}

			try {
				System.out.println("TestTime:" + data[i][9]);
				testStandardSubiterm
						.setTestTime(Double.parseDouble(data[i][9]));
				totleTime = totleTime+Double.parseDouble(data[i][9]);
			} catch (Exception e) {
				System.out.println("数据错误");
				testStandardSubiterm.setTestTime(-1000);
				totleTime = totleTime+0.0;
			}
			
			System.out.println("totleTime:"+totleTime);
			if(data[i][8] == null || data[i][8] == ""){
				testStandardSubiterm.setAddUserName(" ");
			}else{
				testStandardSubiterm.setAddUserName(data[i][10]);
			}
			
			try {
				testStandardSubiterm.setAddUser(testStandardSubitermService
						.getUsernoByName(data[i][10]));
			} catch (Exception e) {
				System.out.println("设置添加人失败");
				testStandardSubiterm.setAddUser("");
			}
			
			

			result = testStandardSubitermService
					.importByExcel(testStandardSubiterm);
			if(result==1){
				if(testStandardSubiterm.getStandardItermID() !=0){
					System.out.println("更新ItemTime");
					System.out.println("id:"+testStandardSubiterm.getStandardItermID());
//					testStandardSubitermService.updateItemTimeWhenExcel(testStandardSubiterm.getStandardItermID(), totleTime);
					testStandardSubitermService.updateItemTime(testStandardSubiterm.getStandardItermID());
				}
			}
		}
		
		

		return result;
//		return 1;
	}

}
